package router

import (
	"5-19yk/client/client"
	"5-19yk/client/requset"
	"5-19yk/client/response"
	"5-19yk/common/pkg"
	"5-19yk/common/proto/doctor"
	"github.com/gin-gonic/gin"
	"path/filepath"
)

func SendSms(c *gin.Context) {
	var data requset.SendSms
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseError(c, err.Error())
		return
	}
	sms, err := client.SendSms(c, &doctor.SendSmsReq{
		Phone:  data.Phone,
		SouRes: data.SouRes,
	})
	if err != nil {
		response.ResponseSuccess(c, err.Error())
		return
	}
	response.ResponseSuccess(c, sms)
}

func Login(c *gin.Context) {
	var data requset.Login
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseError(c, err.Error())
		return
	}
	login, err := client.Login(c, &doctor.LoginReq{
		Name:     data.Name,
		Phone:    data.Phone,
		Password: data.Password,
		Email:    data.Email,
		SendCode: data.SendCode,
	})
	if err != nil {
		response.ResponseError(c, err.Error())
		return
	}
	response.ResponseSuccess(c, login)
}

func Upload(c *gin.Context) {
	file, err := c.FormFile("file")
	if err != nil {
		response.ResponseError(c, err.Error())
		return
	}
	if file.Size > 3*1024*1024 {
		response.ResponseSuccess(c, "文件大小错误")
		return
	}
	Ext := filepath.Ext(file.Filename)
	m := map[string]bool{
		".png": true,
		".jpg": true,
	}
	if !m[Ext] {
		response.ResponseError(c, "文件类型错误")
		return
	}
	dst := "common/upload/status" + Ext
	c.SaveUploadedFile(file, dst)
	pkg.Upload(dst, file.Filename)
	response.ResponseSuccess(c, "文件上传成功"+pkg.GetUrl(file.Filename))

}
func HospitalList(c *gin.Context) {
	var data requset.Hospital
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseError(c, err)
		return
	}
	hospital, err := client.HospitalList(c, &doctor.HospitalListReq{
		Page:         int64(data.Page),
		Size:         int64(data.Size),
		HospitalName: data.HospitalName,
	})
	if err != nil {
		response.ResponseError(c, err.Error())
		return
	}
	response.ResponseSuccess(c, hospital)
}

func DepartmentList(c *gin.Context) {
	var data requset.DepartmentList
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseError(c, err)
		return
	}
	hospital, err := client.DepartmentList(c, &doctor.DepartmentListReq{
		Page:         int64(data.Page),
		Size:         int64(data.Size),
		HospitalName: data.DepartmentName,
	})
	if err != nil {
		response.ResponseError(c, err.Error())
		return
	}
	response.ResponseSuccess(c, hospital)
}
func PatientList(c *gin.Context) {
	list, err := client.PatientList(c, &doctor.PatientListReq{})
	if err != nil {
		response.ResponseError(c, err.Error())
		return
	}
	response.ResponseSuccess(c, list)
}
func DoctorList(c *gin.Context) {
	list, err := client.DoctorList(c, &doctor.DoctorListReq{})
	if err != nil {
		response.ResponseError(c, err.Error())
		return
	}
	response.ResponseSuccess(c, list)
}

func OrderCreate(c *gin.Context) {
	ui := c.GetUint("userId")
	var data requset.OrderCreate
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseError(c, err.Error())
		return
	}
	create, err := client.OrderCreate(c, &doctor.OrderCreateReq{
		DepartmentId: int64(data.DepartmentId),
		UserId:       int64(ui),
		DocTorId:     int64(data.DocTorId),
		HospitalId:   int64(data.HospitalId),
		PatientId:    int64(data.PatientId),
	})
	if err != nil {
		response.ResponseError(c, err.Error())
		return
	}
	response.ResponseSuccess(c, create)
}
func OrderList(c *gin.Context) {
	list, err := client.OrderList(c, &doctor.OrderListReq{})
	if err != nil {
		response.ResponseError(c, err.Error())
		return
	}
	response.ResponseSuccess(c, list)
}
